package com.ruoyi.energize.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.energize.domain.SeriesVo;
import com.ruoyi.energize.domain.YancaoStoreDailySalesReport;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.energize.domain.CashOrderGoods;
import com.ruoyi.energize.service.ICashOrderGoodsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单商品信息表Controller
 *
 * @author ruoyi
 * @date 2023-05-26
 */
@RestController
@RequestMapping("/energize/cash_goods")
public class CashOrderGoodsController extends BaseController
{
    @Autowired
    private ICashOrderGoodsService cashOrderGoodsService;

    /**
     * 查询订单商品信息表列表
     */
    @PreAuthorize("@ss.hasPermi('energize:cash_goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(CashOrderGoods cashOrderGoods)
    {
        startPage();
        List<CashOrderGoods> list = cashOrderGoodsService.selectCashOrderGoodsList(cashOrderGoods);
        return getDataTable(list);
    }

    /**
     * 导出订单商品信息表列表
     */
    @PreAuthorize("@ss.hasPermi('energize:cash_goods:export')")
    @Log(title = "订单商品信息表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CashOrderGoods cashOrderGoods)
    {
        List<CashOrderGoods> list = cashOrderGoodsService.selectCashOrderGoodsList(cashOrderGoods);
        ExcelUtil<CashOrderGoods> util = new ExcelUtil<CashOrderGoods>(CashOrderGoods.class);
        util.exportExcel(response, list, "订单商品信息表数据");
    }

    /**
     * 获取订单商品信息表详细信息
     */
    @PreAuthorize("@ss.hasPermi('energize:cash_goods:query')")
    @GetMapping(value = "/{autoId}")
    public AjaxResult getInfo(@PathVariable("autoId") Long autoId)
    {
        return success(cashOrderGoodsService.selectCashOrderGoodsByAutoId(autoId));
    }

    /**
     * 新增订单商品信息表
     */
    @PreAuthorize("@ss.hasPermi('energize:cash_goods:add')")
    @Log(title = "订单商品信息表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CashOrderGoods cashOrderGoods)
    {
        return toAjax(cashOrderGoodsService.insertCashOrderGoods(cashOrderGoods));
    }

    /**
     * 修改订单商品信息表
     */
    @PreAuthorize("@ss.hasPermi('energize:cash_goods:edit')")
    @Log(title = "订单商品信息表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CashOrderGoods cashOrderGoods)
    {
        return toAjax(cashOrderGoodsService.updateCashOrderGoods(cashOrderGoods));
    }

    /**
     * 删除订单商品信息表
     */
    @PreAuthorize("@ss.hasPermi('energize:cash_goods:remove')")
    @Log(title = "订单商品信息表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{autoIds}")
    public AjaxResult remove(@PathVariable Long[] autoIds)
    {
        return toAjax(cashOrderGoodsService.deleteCashOrderGoodsByAutoIds(autoIds));
    }

    /**
     * 根据日期范围获取到这个范围内卷烟销售额占比/销售数量占比
     * @param cashOrderGoods
     * @return
     */
    @PreAuthorize("@ss.hasPermi('energize:daliySalesReport:list')")
    @GetMapping("/listCigaretteSales")
    public Map<String, Object> listCigaretteSales(CashOrderGoods cashOrderGoods) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        //获取某个时间段内卷烟每个名称的销售额占比
        List<CashOrderGoods> list=cashOrderGoodsService.listCigaretteSalesBydate(cashOrderGoods);
        List<String> xAxisVoList=new ArrayList<>();
        List<String> seriesVoList=new ArrayList<>();

        List<String> xAxisVoNumberList=new ArrayList<>();
        List<String> seriesVoNumberList=new ArrayList<>();
        //卷烟销售额饼图数组列表
        List<SeriesVo> seriesPieVoList=new ArrayList<>();

        //卷烟销售数量饼图数组列表
        List<SeriesVo> seriesNumberVoList=new ArrayList<>();
        SeriesVo seriesCigaretteVo=new SeriesVo();
        SeriesVo seriesCigaretteNumberVo=new SeriesVo();

        if(list!=null && list.size()>0){
            for (CashOrderGoods cashOrderGoods1:list){
                //卷烟销售额饼图构造数据
                seriesCigaretteVo.setName(cashOrderGoods1.getName());
                seriesCigaretteVo.setValue(String.valueOf(cashOrderGoods1.getRealUnitPrice()));
                seriesPieVoList.add((SeriesVo)BeanUtils.cloneBean(seriesCigaretteVo));
                //卷烟销售额柱状图构造数据
                xAxisVoList.add(cashOrderGoods1.getName());
                seriesVoList.add(String.valueOf(cashOrderGoods1.getRealUnitPrice()));
                //卷烟销售数量饼图构造数据
                seriesCigaretteNumberVo.setName(cashOrderGoods1.getName());
                seriesCigaretteNumberVo.setValue(String.valueOf(cashOrderGoods1.getCount()));
                seriesNumberVoList.add((SeriesVo)BeanUtils.cloneBean(seriesCigaretteNumberVo));
                //卷烟销售数量柱状图构造数据
                xAxisVoNumberList.add(cashOrderGoods1.getName());
                seriesVoNumberList.add(String.valueOf(cashOrderGoods1.getCount()));

            }
        }

        Map<String, Object> res=new HashMap();
        res.put("seriesPieVoList",seriesPieVoList);
        res.put("xAxisVoList",xAxisVoList);
        res.put("seriesVoList",seriesVoList);

        res.put("seriesNumberVoList",seriesNumberVoList);
        res.put("xAxisVoNumberList",xAxisVoNumberList);
        res.put("seriesVoNumberList",seriesVoNumberList);
        return res;
    }


    @PreAuthorize("@ss.hasPermi('energize:daliySalesReport:list')")
    @GetMapping("/getCigarettePriceRange")
    public Map<String, Object> getCigarettePriceRange(CashOrderGoods cashOrderGoods) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        //获取某个时间段内卷烟每个名称的销售额占比
        List<CashOrderGoods> list=cashOrderGoodsService.getCigarettePriceRange(cashOrderGoods);
        //卷烟销售额饼图数组列表
        List<SeriesVo> seriesPieVoList=new ArrayList<>();
        //卷烟销售数量饼图数组列表
        SeriesVo seriesCigaretteVo=new SeriesVo();
        Double Twenty= Double.valueOf(0);//20（含）及以下
        Double Fiffy=Double.valueOf(0);//20-50（含）
        Double FiffyUp=Double.valueOf(0);//50以上
        if(list!=null && list.size()>0){
            for (CashOrderGoods cashOrderGoods1:list){
                //将某个价格区间的值加起来
                Twenty+=cashOrderGoods1.getPurPrice().doubleValue();
                Fiffy+=cashOrderGoods1.getBigPrice1().doubleValue();
                FiffyUp+=cashOrderGoods1.getBigPrice2().doubleValue();
            }
        }
        //卷烟价格区间饼图构造数据
        seriesCigaretteVo.setName("20元（含）以下");
        seriesCigaretteVo.setValue(String.valueOf(Twenty));
        seriesPieVoList.add((SeriesVo)BeanUtils.cloneBean(seriesCigaretteVo));
        seriesCigaretteVo.setName("20元-50元（含）");
        seriesCigaretteVo.setValue(String.valueOf(Fiffy));
        seriesPieVoList.add((SeriesVo)BeanUtils.cloneBean(seriesCigaretteVo));
        seriesCigaretteVo.setName("50元以上");
        seriesCigaretteVo.setValue(String.valueOf(FiffyUp));
        seriesPieVoList.add((SeriesVo)BeanUtils.cloneBean(seriesCigaretteVo));
        Map<String, Object> res=new HashMap();
        res.put("seriesPieVoList",seriesPieVoList);
        return res;
    }
}
